/*
	This file is part of "Ants on the web".

    "Ants on the web" is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "Ants on the web" is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with "Ants on the web".  If not, see <http://www.gnu.org/licenses/>.
*/

package org.utc.ia04.antsontheweb.gui.knowledgeBase;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import org.utc.ia04.antsontheweb.knowledgeBase.KnowledgeBaseAgent;


import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.gui.GuiAgent;
import jade.gui.GuiEvent;
import jade.lang.acl.ACLMessage;

public class KnowledgeBaseLearningGuiAgent extends GuiAgent
{
	//serialVersionUID
	private static final long serialVersionUID = -7819381520385686311L;

	//GuiEvent
	static public final int LEARN = 0;
	static public final int TEST = 1;
	
	//Property change
	static public final String LEARNING_COMPLETE = "Learning complete...";
	static public final String TRUE = "KB answered : YES...";
	static public final String FALSE = "KB answered : NO...";
	static public final String ERROR_OCCURED = "Error occured...";
	
	//Attributs
	private PropertyChangeSupport changes;
	private KnowledgeBaseLearningFrame frame;
	
	//METHODES
	public void setup()
	{
		changes = new PropertyChangeSupport(this);
		frame = new KnowledgeBaseLearningFrame(this);
		addBehaviour(new KnowledgeBaseLearningGuiAgentBhv());
	}
	
	public void addPropertyChangeListener(PropertyChangeListener listener)
	{
		changes.addPropertyChangeListener(listener);
	}
	
	protected void onGuiEvent(GuiEvent e)
	{
		if(   e.getType() == KnowledgeBaseLearningGuiAgent.LEARN
		   || e.getType() == KnowledgeBaseLearningGuiAgent.TEST)
		{
			ACLMessage message = new ACLMessage(ACLMessage.REQUEST);
			message.setContent((String)e.getParameter(0));
			
			try
			{
				DFAgentDescription template = new DFAgentDescription();
				ServiceDescription sd = new ServiceDescription();
				sd.setType(KnowledgeBaseAgent.dfType);
				template.addServices(sd);
				DFAgentDescription[] result = DFService.search(this, template);
				for(int i = 0; i < result.length; ++i)
					message.addReceiver(result[i].getName());
				send(message);
			}
			catch(Exception ex)
			{
				System.out.println("Erreur sending request : " + ex.toString());
			}
		}
	}
	
	public void notifyFrame(String message)
	{
		changes.firePropertyChange(message, ".", "");
	}
	
	public void loadPage(String page)
	{
		frame.loadPage(page);
	}
	
	public void takeDown()
	{
		DFAgentDescription dfd = new DFAgentDescription();
		dfd.setName(getAID());

		try { DFService.deregister(this, dfd); }
		catch (FIPAException e) { }
	}
}
